package com.wkt.ggkt.vod.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wkt.ggkt.exception.GgktException;
import com.wkt.ggkt.model.vod.Subject;
import com.wkt.ggkt.vo.vod.SubjectEeVo;
import com.wkt.ggkt.vod.listener.SubjectListener;
import com.wkt.ggkt.vod.mapper.SubjectMapper;
import com.wkt.ggkt.vod.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author wkt
 * @since 2022-07-03
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {


    @Autowired
    private SubjectListener subjectListener;

    @Override
    public List<Subject> queryChildSubjects(Long id) {

        QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        List<Subject> subjects = baseMapper.selectList(queryWrapper);
        //判断当前节点下是否还有子节点
        for (Subject subject : subjects) {
            Long subjectId = subject.getId();
            Integer count = baseMapper.selectCount(new QueryWrapper<Subject>().eq("parent_id", subjectId));
            if (count > 0) {
                subject.setHasChildren(true);
            }
        }
        return subjects;
    }

    @Override
    public void exportData(HttpServletResponse response) {
        //设置下载信息
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("课程分类", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

            List<Subject> subjects = baseMapper.selectList(null);
            List<SubjectEeVo> list = new ArrayList();
            for (Subject subject : subjects) {
                SubjectEeVo subjectEeVo = new SubjectEeVo();
                BeanUtils.copyProperties(subject, subjectEeVo);
                list.add(subjectEeVo);
            }

            EasyExcel.write(response.getOutputStream(),SubjectEeVo.class)
                    .sheet("课程分类")
                    .doWrite(list);
        } catch (Exception e) {
            throw new GgktException(20001,"导出失败");
        }
    }

    @Override
    public void importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),SubjectEeVo.class,subjectListener).sheet().doRead();
        } catch (IOException e) {
            throw new GgktException(20001,"导入失败");
        }
    }


}
